package vn.vietel.pms.web.action;

import static org.jboss.seam.ScopeType.EVENT;

import java.util.List;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.annotations.datamodel.DataModelSelection;
import org.jboss.seam.faces.FacesMessages;

import vn.vietel.pms.business.UserServiceImpl;
import vn.vietel.pms.entity.User;

@Scope(EVENT)
@Name("register")
public class RegisterAction {

	@In(required = false)
	@DataModelSelection
	private User user;

	@In("#{userService}")
	private UserServiceImpl userService;

	@In
	private FacesMessages facesMessages;

	private String verify;

	private boolean registered;

	@DataModel
	private List<User> users;
	
	public void register() {
		if (user.getPassword().equals(verify)) {
			try {
				userService.saveOrUpdate(user);				
				registered = true;
				users = userService.selectAll();
			} catch (vn.vietel.pms.entity.exception.ValidationException e) {
				facesMessages.add(e.getMessage());
			}
		} else {
			facesMessages.add("verify", "Re-enter your password");
			verify = null;
		}
	}

	public void invalid() {
		facesMessages.add("Please try again");
	}

	public String getVerify() {
		return verify;
	}

	public void setVerify(String verify) {
		this.verify = verify;
	}

	public boolean isRegistered() {
		return registered;
	}

	public void setRegistered(boolean registered) {
		this.registered = registered;
	}
	
	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

}
